package com.hakim.wfb.web.controller.admin;

/**
 * Created by Administrator on 2016/11/14.
 */

import com.hakim.wfb.admin.params.admin.*;
import com.hakim.wfb.admin.result.admin.*;
import com.hakim.wfb.admin.service.api.SystemAuthService;
import com.hakim.wfb.admin.service.api.SystemUserActionService;
import com.hakim.wfb.web.contants.Constants;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.session.SessionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liuyi
 *         2016/11/14 13:39
 */
@RestController
@RequestMapping(value = "/system", name = "用户登陆、权限、菜单管理")
public class UserAuthorityController {

    @Autowired
    private SystemUserActionService systemUserActionService;

    @Autowired
    private SystemAuthService systemAuthService;

    @RequestMapping(value = "/get_menu", name = "获取")
    public ListResultEx<AdminMenuView> getMenus(GetAdminMenuParam param) {
        ListResultEx<AdminMenuView> result = systemUserActionService.getUserMenu(param);
        return result;
    }

    /**
     * 登录 test
     *
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "/login", name = "后台用户登录", method = {RequestMethod.POST})
    public ResultEx userLogin(LoginParam param, HttpServletResponse response) {
        ObjectResultEx<SessionIdentity> result = systemUserActionService.login(param);
        if (result.isFailed()) {
            return result;
        }
        SessionHandler sessionHandler = new SessionHandler();
        String token = sessionHandler.setSessionIdentity(result.getDataObject(), Long.valueOf(Constants.SYSTEM_SESSION_EXPIRE));
        sessionHandler.login(token, response, Long.valueOf(Constants.SYSTEM_SESSION_EXPIRE));

        return new ResultEx().makeSuccessResult();
    }

    /**
     * 登出
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/logout", name = "登出", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResultEx userLogout(HttpServletRequest request, HttpServletResponse response) {
        SessionHandler sessionHandler = new SessionHandler();
        sessionHandler.logout(request, response);
        return new ResultEx().makeSuccessResult();
    }

    //region role 角色

    /**
     * 添加角色
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/role/add", name = "添加角色", method = {RequestMethod.POST})
    public ObjectResultEx<RoleView> addRole(AddRoleParam param) {
        return systemAuthService.addRole(param);
    }

    /**
     * 获取单条角色信息，包含菜单及权限信息1
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/role/get", name = "获取单条角色信息，包含菜单及权限信息", method = {RequestMethod.GET, RequestMethod.POST})
    public ObjectResultEx<RoleView> getRole(GetRoleParam param) {
        return systemAuthService.getRole(param);
    }

    /**
     * 修改角色
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/role/mod", name = "修改角色", method = {RequestMethod.POST, RequestMethod.PUT})
    public ResultEx modRole(ModifyRoleParam param) {
        return systemAuthService.modifyRole(param);
    }

    /**
     * 删除角色
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/role/delete", name = "删除角色", method = {RequestMethod.DELETE, RequestMethod.POST})
    public ResultEx delRole(DeleteRoleParam param) {
        return systemAuthService.deleteRole(param);
    }

    /**
     * 查询角色列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/role/query", name = "查询角色列表", method = {RequestMethod.GET, RequestMethod.POST})
    public ListResultEx<RoleFullView> queryRole(QueryRoleParam param) {
        return systemAuthService.queryRoleList(param);
    }

    //endregion//

    //region menu 菜单

    /**
     * 添加菜单项
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/menu/add", name = "添加菜单项", method = {RequestMethod.POST})
    public ObjectResultEx<MenuFullView> addMenu(AddMenuParam param) {
        return systemAuthService.addMenu(param);
    }

    /**
     * 获取单条菜单信息
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/menu/get", name = "获取单条菜单信息", method = {RequestMethod.GET, RequestMethod.POST})
    public ObjectResultEx<MenuFullView> getMenu(GetMenuParam param) {
        return systemAuthService.getMenu(param);
    }

    /**
     * 修改菜单信息
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/menu/mod", name = "修改菜单信息", method = {RequestMethod.PUT, RequestMethod.POST})
    public ResultEx modMenu(ModifyMenuParam param) {
        return systemAuthService.modifyMenu(param);
    }

    /**
     * 查询菜单列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/menu/query", name = "查询菜单列表", method = {RequestMethod.GET, RequestMethod.POST})
    public ListResultEx<QueryMenuView> queryMenu(QueryMenuParam param) {
        return systemAuthService.queryMenuList(param);
    }

    /**
     * 删除菜单项
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/menu/delete", name = "删除菜单项", method = {RequestMethod.DELETE, RequestMethod.POST})
    public ResultEx delMenu(DeleteMenuParam param) {
        return systemAuthService.deleteMenu(param);
    }
    //endregion

    //region user 用户

    /**
     * 添加用户
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/user/add", name = "添加用户", method = {RequestMethod.POST})
    public ObjectResultEx<AdminFullView> addUser(AddAdminParam param) {
        return systemAuthService.addUser(param);
    }

    /**
     * 获取单条用户信息
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/user/get", name = "获取单条用户信息", method = {RequestMethod.GET, RequestMethod.POST})
    public ObjectResultEx<AdminFullView> getUser(GetAdminParam param) {
        return systemAuthService.getUser(param);
    }

    /**
     * 修改用户信息
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/user/mod", name = "修改用户信息", method = {RequestMethod.PUT, RequestMethod.POST})
    public ResultEx modUser(ModifyAdminParam param) {
        return systemAuthService.modifyUser(param);
    }

    /**
     * 获取用户列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/user/query", name = "获取用户列表", method = {RequestMethod.GET, RequestMethod.POST})
    public ListResultEx<QueryAdminView> queryUser(QueryAdminParam param) {
        return systemAuthService.queryUserList(param);
    }

    /**
     * 获取用户及角色信息
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/user/get_user_roles", name = "获取用户及角色信息", method = {RequestMethod.GET, RequestMethod.POST})
    public ObjectResultEx<AdminRolesResultView> getUserRoles(GetAdminParam param) {
        return systemAuthService.queryUserRoles(param);
    }

    /**
     * 获取用户菜单
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/user/get_user_menu", name = "获取用户菜单")
    public ListResultEx<AdminMenuResultView> getUserMenu(GetAdminMenuParam param) {
        return systemAuthService.getUserMenu(param);
    }


    //endregion
}
